﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RM2012Service.DataService;
using System.Data;
using System.Data.SqlClient;
using RM2012Service.Business;

namespace RM2012Service.DataAccess
{
    public class InvoiceDAO
    {
        DBConnection db;

        public InvoiceDAO()
        {
            db = new DBConnection();
        }

        public DataSet GetAllInvoice()
        {
            return db.ExecuteDataSet("GetAllInvoiceFull", "Invoice", null);
        }

        public DataSet GetAllInvoicePaid()
        {
            return db.ExecuteDataSet("GetInvoiceIsPaidFull", "Invoice", null);
        }

        public DataSet GetAllInvoiceNotPaid()
        {
            return db.ExecuteDataSet("GetInvoiceNotPaidFull", "Invoice", null);
        }

        public DataSet GetAllInvoiceById(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param.Value = invoiceId;

            return db.ExecuteDataSet("GetInvoiceByIdFull", "Invoice", param);
        }

        public int Add(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@InvoiceDate", SqlDbType.DateTime);
            param[1].Value = info.InvoiceDate;

            param[2] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[2].Value = info.CusId;

            param[3] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[3].Value = info.TablesId;

            param[4] = new SqlParameter("@Total", SqlDbType.Money);
            param[4].Value = info.Total;

            param[5] = new SqlParameter("@Paid", SqlDbType.Money);
            param[5].Value = info.Paid;

            param[6] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[6].Value = info.IsDeleted;

            param[7] = new SqlParameter("@IsPaid", SqlDbType.Bit);
            param[7].Value = info.IsPaid;

            param[8] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[8].Value = info.DiscountPer;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return db.ExecuteNoneQuery("AddInvoice", param);
        }

        public int Update(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[10];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@InvoiceDate", SqlDbType.DateTime);
            param[1].Value = info.InvoiceDate;

            param[2] = new SqlParameter("@CusId", SqlDbType.VarChar, 6);
            param[2].Value = info.CusId;

            param[3] = new SqlParameter("@TablesId", SqlDbType.VarChar, 6);
            param[3].Value = info.TablesId;

            param[4] = new SqlParameter("@Total", SqlDbType.Money);
            param[4].Value = info.Total;

            param[5] = new SqlParameter("@Paid", SqlDbType.Money);
            param[5].Value = info.Paid;

            param[6] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[6].Value = info.IsDeleted;

            param[7] = new SqlParameter("@IsPaid", SqlDbType.Bit);
            param[7].Value = info.IsPaid;

            param[8] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[8].Value = info.DiscountPer;

            param[9] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[9].Value = info.Remark;

            return db.ExecuteNoneQuery("UpdateInvoice", param);
        }

        public int UpdateTotal(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@Total", SqlDbType.Money);
            param[1].Value = info.Total;

            return db.ExecuteNoneQuery("UpdateInvoiceTotal", param);
        }

        public int UpdatePaid(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@Paid", SqlDbType.Money);
            param[1].Value = info.Paid;

            param[2] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[2].Value = info.Remark;

            return db.ExecuteNoneQuery("UpdateInvoicePaid", param);
        }

        public int UpdateNeeded(InvoiceInfo info)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 14);
            param[0].Value = info.InvoiceId;

            param[1] = new SqlParameter("@Paid", SqlDbType.Money);
            param[1].Value = info.Paid;

            param[2] = new SqlParameter("@IsDeleted", SqlDbType.Bit);
            param[2].Value = info.IsDeleted;

            param[3] = new SqlParameter("@IsPaid", SqlDbType.Bit);
            param[3].Value = info.IsPaid;

            param[4] = new SqlParameter("@DiscountPer", SqlDbType.Money);
            param[4].Value = info.DiscountPer;

            param[5] = new SqlParameter("@Remark", SqlDbType.NVarChar, 500);
            param[5].Value = info.Remark;

            return db.ExecuteNoneQuery("UpdateInvoiceNeeded", param);
        }

        public int Delete(String invoiceId)
        {
            SqlParameter param = new SqlParameter("@InvoiceId", SqlDbType.VarChar, 6);
            param.Value = invoiceId;

            return db.ExecuteNoneQuery("DeleteInvoice", param);
        }
    }
}
